<template>
  <div class="DecRoot">
    <el-card class="box-card">
      <div class="headerBox">
        <div class="leftBox">帮助中心</div>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="dialogVisible = true"
          plain
          >新增</el-button
        >
      </div>
      <div class="search">
        <el-form :inline="true"  >
          <el-form-item label="标题">
            <el-input v-model="ipageCofig.keyWord"></el-input>
          </el-form-item>
          <el-button size="small" style="margin-top: 3px;" type="primary" @click="searchList()" >查询</el-button>
        </el-form>

      </div>
    </el-card>
    <el-card class="box-card newCard">
      <el-table
        :data="tableData"
        style="width: 100%"
        height="66vh"
        header-cell-class-name="tableHeader"
        stripe
      >
        <el-table-column label="序号" type="index" width="50">
        </el-table-column>
        <el-table-column prop="name" label="标题"> </el-table-column>
        <el-table-column prop="value" min-width="180" label="内容">
          <template slot-scope="scope">
            <div v-html="scope.row.value"></div>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <!-- {{this.$moment(scope.row.createTime).format("YYYY/MM/DD HH:mm:ss")}} -->
            {{ Moment(scope.row.createTime).format("YYYY/MM/DD HH:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" width="100">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="编辑"
              placement="top"
            >
              <img
                src="@/assets/image/icon/edit.png"
                @click="editChange(scope.row)"
                class="iconImg"
                alt=""
              />
            </el-tooltip>
            <el-popconfirm
              title="确定要删除该条帮助信息吗？"
              @confirm="deleteContent(scope.row)"
            >
              <!-- <el-button slot="reference">

              </el-button> -->
              <template slot="reference">
                <i
                  class="el-icon-delete"
                  style="font-size: 25px; color: #409eff; cursor: pointer"
                ></i>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <div></div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="ipageCofig.pageSize"
          :current-page.sync="ipageCofig.pageNumber"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <el-dialog :visible.sync="dialogVisible" width="500" class="newDialog">
      <div class="DiaLogHeader">
        <div class="titlebOX">
          {{ form.id ? "编辑帮助信息" : "新增帮助信息" }}
        </div>
        <div class="chahao" @click="dialogVisible = false">
          <i class="el-icon-close"></i>
        </div>
      </div>
      <el-divider></el-divider>
      <div style="padding: 10px">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="标题">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="内容">
            <ckeditor4 :data.sync="form.value"></ckeditor4>
          </el-form-item>
        </el-form>
      </div>
      <div class="btnBox">
        <el-button type="primary" @click="addSubmit"> 确定</el-button>
        <el-button @click="closeDialog"> 取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ckeditor4 from '@/components/ckeditor4'
import {
  getHelpList,
  addHelpContent,
  editHelpContent,
  deleteHelpContent
} from '@/api/helpCenterApi.js'
import Moment from 'moment'

export default {
  components: { ckeditor4 },
  name: 'helpCenter',
  data () {
    return {
      form: {
        id: '',
        name: '',
        value: ''
      },
      dialogVisible: false,
      tableData: [],
      total: 0,
      ipageCofig: {
        keyWord: '',
        pageNumber: 1,
        pageSize: 10
      }
    }
  },
  created () {
    this.getPageList()
  },
  methods: {
    Moment,
    //
    async getPageList () {
      const result = await getHelpList(this.ipageCofig)
      if (result.code === 200) {
        const { data } = result
        this.tableData = data.records
        this.total = data.total
      }
    },
    searchList () {
      this.ipageCofig.pageNumber = 1
      this.getPageList()
    },
    // 编辑
    addSubmit () {
      if (this.form.id) {
        this.editHelp()
      } else {
        delete this.form.id
        this.addHandleChange()
      }
    },
    // 新增
    async addHandleChange () {
      const result = await addHelpContent(this.form)
      if (result.data) {
        this.$message.success('新增成功')
        this.closeDialog()
        this.getPageList()
      }
    },
    editChange (row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    //
    async editHelp () {
      const result = await editHelpContent(this.form)
      if (result.code == 200) {
        this.$message.success('编辑成功')
        this.closeDialog()
        this.getPageList()
      }
    },
    async deleteContent (row) {
      const params = {
        idList: row.id
      }
      const { data, code } = await deleteHelpContent(params)
      if (data) {
        this.closeDialog()
        this.getPageList()
      }
    },
    // 分页
    handleSizeChange (val) {
      this.ipageCofig.pageSize = val
      this.getPageList()
    },
    handleCurrentChange (val) {
      this.ipageCofig.pageNumber = val
      this.getPageList()
    },
    closeDialog () {
      this.dialogVisible = false
      const params = {
        id: '',
        name: '',
        value: '',
        createTime: ''
      }
      this.form = Object.assign({}, params)
    }
  },
  watch:{
    dialogVisible(){
      if(!this.dialogVisible){
        this.form.name=''
        this.form.value='',
        this.form.id=null
      }
    }
  }
}
</script>

<style scoped lang="scss">
.btnBox {
  display: flex;
  justify-content: center;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 10px 0;

  .btnImg {
    width: 14px;
    height: 14px;
    vertical-align: middle;
  }
}

.DecRoot {
  padding: 20px;
  padding-left: 5px;
  padding-bottom: 0;

  .newCard {
    height: 75vh;
    margin-top: 20px;
  }

  /deep/ .el-button {
    padding: 9px 15px;
  }

  .el-card {
    .headerBox {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .leftBox {
        display: flex;
        align-items: center;
        font-size: 18px;
        font-weight: 700;

        /deep/ .el-form-item {
          margin-bottom: 0;
        }

        /deep/ .el-input--suffix {
          width: 200px;
        }
      }
    }

    /deep/ .tableHeader {
      background: #edf3ff;
      font-weight: 700;
      color: #666666;
    }

    .iconImg {
      width: 20px;
      height: 20px;
      cursor: pointer;
      margin-right: 10px;
    }

    .pagination {
      display: flex;
      justify-content: space-between;
      margin-top: 10px;

      /deep/ .el-pagination__editor.el-input .el-input__inner {
        height: 28px !important;
      }

      /deep/ .el-input--mini .el-input__inner {
        height: 28px !important;
      }
    }
  }
}
</style>
  <style scoped lang="scss">
  .search{
    display: flex;

  }
.newDialog {
  /deep/ .el-divider {
    margin: 0px;
  }
  /deep/ .el-dialog__header {
    padding: 0;
  }
  /deep/ .el-form-item__content {
    width: 75%;
  }
  /deep/ .el-select {
    width: 100%;
  }
  /deep/ .tableHeader {
    background: #edf3ff;
    font-weight: 700;
    color: #666666;
    padding: 5px 0;
  }
  .btnBox {
    display: flex;
    justify-content: center;
    background: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 10px 0;
    .btnImg {
      width: 14px;
      height: 14px;
      vertical-align: middle;
    }
    /deep/ .el-button {
      padding: 10px 20px;
    }
  }
  .DiaLogHeader {
    display: flex;
    padding: 10px 20px;
    justify-content: space-between;
    .titlebOX {
      font-size: 16px;
      font-weight: 700;
    }
    .chahao {
      width: 20px;
      height: 20px;
      background-image: url("@/assets/image/icon/chahao.png");
      background-size: 100% 100%;
      text-align: center;
      line-height: 20px;
      color: #e65f00;
      cursor: pointer;
    }
  }
  /deep/ .el-input {
    width: 100% !important;
  }
}
/deep/ .el-dialog__header {
  font-weight: 700;
}
/deep/ .demo-ruleForm {
  padding: 10px;
}

/deep/ .el-form-item {
  margin-bottom: 8px;
}
/deep/ .el-form-item__error {
  z-index: 99;
}
/deep/ .el-dialog__body {
  padding: 0;
  padding-top: 10px;
  padding-bottom: 60px;
}
.dialogTextBox {
  font-size: 14px;
  color: #000000;
}
</style>
